﻿using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Layout;
using iOSClub.App.Share.WeatherModels;

namespace iOSClub.App.Desktop.DataTemplates;

public class WarningDataTemplate : IDataTemplate
{
    private Dictionary<string, string> ColorChinese => new()
    {
        { "White", "白色" },
        { "Blue", "蓝色" },
        { "Green", "绿色" },
        { "Yellow", "黄色" },
        { "Orange", "橙色" },
        { "Red", "红色" },
        { "Black", "黑色" }
    };

    public Control? Build(object? param)
    {
        if (param is not WeatherWarning warning) return null;
        if (warning.type == "") return null;
        var border = new Border();
        border.Classes.Add("Card");
        var icon = new Avalonia.Svg.Skia.Svg(
            new Uri("avares://iOSClub.App.Desktop/Assets/"))
        {
            Path = $"/Assets/WeatherIcon/{warning.type}.svg", Height = 25
        };
        if (!string.IsNullOrEmpty(warning.severityColor))
            icon.Classes.AddRange([warning.severityColor]);

        var text = new TextBlock()
        {
            Text =
                $"{warning.typeName}" +
                $"{(string.IsNullOrEmpty(warning.severityColor) ? "" : ColorChinese[warning.severityColor])}" +
                $"预警",
            Margin = new Thickness(5),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };
        var panel = new WrapPanel()
        {
            Children = { icon, text }
        };
        border.Child = panel;
        return border;
    }

    public bool Match(object? data) => data is WeatherWarning;
}